package com.appkubes.mall.restful.application.promoter.event.impl;

import com.appkubes.mall.core.entity.MallOrder;
import com.appkubes.mall.core.entity.MallOrderGoods;
import com.appkubes.mall.core.entity.MallPromoter;
import com.appkubes.mall.core.entity.MallUserHierarchy;
import com.appkubes.mall.restful.application.order.goods.service.OrderGoodsService;
import com.appkubes.mall.restful.application.order.order.service.OrderService;
import com.appkubes.mall.restful.application.promoter.event.PromoterHandle;
import com.appkubes.mall.restful.application.promoter.service.PromoterService;
import com.appkubes.mall.restful.application.user.hierarchy.service.UserHierarchyService;
import com.appkubes.mall.restful.event.order.OrderAddEvent;
import com.appkubes.mall.restful.event.order.entity.OrderAddEventEO;
import com.appkubes.mall.restful.event.statis.PageViewStatisEvent;
import com.appkubes.mall.restful.event.statis.entity.PageViewEventEO;
import com.appkubes.mall.restful.event.user.UserRegisterEvent;
import com.appkubes.mall.restful.event.user.entity.UserRegisterEventEO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @Author: Rongjie
 * @Date: 2020/3/6
 * @Description:
 */
@Repository
@Slf4j
public class PromoterHandleImpl implements PromoterHandle {
    @Resource
    PromoterService promoterService;
    @Resource
    UserHierarchyService userHierarchyService;
    @Resource
    OrderService orderService;

    //处理订单增加事件
    @EventListener
    public void orderAdd(OrderAddEvent event) {
        OrderAddEventEO addEventEO = event.getOrderAddEventEO();

        MallUserHierarchy hierarchy = userHierarchyService.getById(addEventEO.getUserKey());
        if (hierarchy == null) {
            return;
        }
        String promoterKey = hierarchy.getPromoterKey();
        if (promoterKey == null) {
            return;
        }
        MallPromoter promoter = promoterService.getById(promoterKey);
        if (promoter == null) {
            return;
        }
        //统计订单数
        promoter.setStatisOrderCount(promoter.getStatisOrderCount() + 1);
        promoter.setStatisOrderAmountCount(addEventEO.getAmount().add(promoter.getStatisOrderAmountCount()));

        if (!StringUtils.isEmpty(addEventEO.getOrderKey())) {
            MallOrder mallOrder = orderService.getById(addEventEO.getOrderKey());
            if (mallOrder != null) {
                promoter.setStatisOrderOrinalAmountCount(mallOrder.getUseDepositMoney().add(promoter.getStatisOrderOrinalAmountCount()));
            }
        }
        promoterService.updateById(promoter);
    }

    //用户注册事件
//    @EventListener
//    public void userRegister(UserRegisterEvent event) {
//        log.info("userRegister event handle: {}", event);
//        UserRegisterEventEO eventEO = event.getEventEO();
//        String promoterKey = eventEO.getPromoterKey();
//        if (promoterKey == null) {
//            return;
//        }
//        MallPromoter promoter = promoterService.getById(promoterKey);
//        if (promoter == null) {
//            return;
//        }
//        //统计注册数
//        promoter.setStatisRegCount(promoter.getStatisRegCount() + 1);
//        promoterService.updateById(promoter);
//    }

    //页面浏览
    @EventListener
    public void pageView(PageViewStatisEvent event) {
        PageViewEventEO pageViewEventEO = event.getPageViewEventEO();
        String promoterKey = pageViewEventEO.getPromoterKey();
        if (promoterKey == null) {
            return;
        }
        MallPromoter promoter = promoterService.getById(promoterKey);
        if (promoter == null) {
            return;
        }
        //页面浏览统计
        promoter.setStatisUvCount(promoter.getStatisUvCount() + 1);
        promoterService.updateById(promoter);
    }

}